import multiprocessing as mp
import threading as td
import time
def job(q):
    res = 0
    for i in range(1000000):
        res += i+i**2+i**3
    q.put(res)

def  multi():
    q = mp.Queue()  # Multiprocessing 进程中不能有return值 所以是放在Queue 这里put
    m1 = mp.Process(target=job, args=(q,)) #创建一个进程 参数和threading一样
    m2 = mp.Process(target=job, args=(q,))
    m1.start()    #参数和threading一样
    m2.start()    #参数和threading一样
    m1.join()    #参数和threading一样
    m2.join()    #参数和threading一样
    res1 = q.get()  # 获取res1的值
    res2 = q.get()
    print('mulit:',res1 + res2)  # 再接着继续运算

def no():
    res = 0
    for _ in range(2):
        for i in range(1000000):
            res += i + i ** 2 + i ** 3
    print('no:',res)
def thin():
    q = mp.Queue()
    t1 = td.Thread(target=job, args=(q,))
    t2 = td.Thread(target=job, args=(q,))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    res1 = q.get()
    res2 = q.get()
    print('thindering:',res1 + res2)

if __name__ == '__main__':
    time_1 = time.time()
    no()
    time_2 = time.time()
    print("什么都没有的时间：",time_2-time_1)
    multi()
    time_3 = time.time()
    print('多进程：',time_3-time_1)
    thin()
    time_4 = time.time()
    print("多线程：",time_4-time_1)
